﻿using log4net;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZeroBug.EasyFlow.Core;
using ZeroBug.EasyFlow.Machine;

namespace ZeroBug.EasyFlow.Step
{
    public class WaitSignalStep : IStep
    {
        public bool IsSingleRun { get; set; }
        public bool IgnoreChildren { get; set; }
        public string Icon { get; set; } = "Megaphone";
        public int ChildrenIndex { get; set; }
        public Core.Step? NextStep { get; set; }

        VariableExpression flowName;
        VariableExpression signalName;
        public bool Compile(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;
            return step.CheckParameters(out errInfo, "FlowName", "SignalName");
        }

        public object GetForm()
        {
            throw new NotImplementedException();
        }

        public StepInfo GetInfo()
        {
            StepInfo info = new StepInfo();
            info.StepName = "WaitSignal";
            info.Group = StepGroup.Control;
            info.ImagePath = "pack://application:,,,/ZeroBug.EasyFlow.Step;component/Images/条件.png";
            return info;
        }

        public string GetTip(Core.Step step)
        {
            return "";
        }

        public void OnChildrenEnter()
        {
        }

        public void OnChildrenLeave()
        {
        }

        public bool Prepare(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = "";
            return true;
        }

        public void RegisterVariables(FlowBase flow, Core.Step step)
        {
        }

        public StepResult Run(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = "";

            string signalNameStr = step.Parameters["SignalName"].GetEquivalent()[0];
            string flowNameStr = step.Parameters["FlowName"].GetEquivalent()[0];

            LocalMachine.Services.GetService<LogHelper>().Log(LogSource.App, $"开始等待来自[{flowNameStr}]的信号[{signalNameStr}]", MessageLevel.Log);
            if (flow.IsStepRun)
            {
                if (!flow.GetFlowManager().CheckSignal(flowNameStr, signalNameStr))
                    return StepResult.Break;
                flow.GetFlowManager().ResetSignal(flowNameStr, signalNameStr);
            }
            else
            {
                while (!flow.GetFlowManager().CheckSignal(flowNameStr, signalNameStr) &&
                       !flow.IsBreak() &&
                       !flow.PauseFlag)
                {
                    Thread.Sleep(20);
                }
                if (flow.IsBreak())
                    return StepResult.Success;
                else if (flow.PauseFlag)
                    return StepResult.Break;
                flow.GetFlowManager().ResetSignal(flowNameStr, signalNameStr);
            }
            LocalMachine.Services.GetService<LogHelper>().Log(LogSource.App, $"收到了来自[{flowNameStr}]的信号[{signalNameStr}]", MessageLevel.Log);
            
            return StepResult.Success;
        }

        public (SerializableDictionary<string, VariableExpression>, List<VariableJudge>) GetParametersAndConditions()
        {
            SerializableDictionary<string, VariableExpression> paras = new SerializableDictionary<string, VariableExpression>();
            paras.Add("FlowName", new VariableExpression("FlowName"));
            paras.Add("SignalName", new VariableExpression("SignalName"));

            return (paras, new List<VariableJudge>());
        }

        public void UnRegisterVariables(FlowBase flow, Core.Step step)
        {
        }
    }
}
